import {
  WorkOrderMyIndexApi
} from "../../../../common/apis/workOrder";
import {
  DIC_KEY
} from "../../../../config/dicKey.config";
import {
  getDicList
} from "../../../../utils/dic";
import {
  WxValidate
} from "../../../../utils/WxValidate";
const mixin_formItemType = require('../../../../common/mixins/formItemType')

Component({
  behaviors: [mixin_formItemType],
  /**
   * 组件的属性列表
   */
  properties: {
    orderNum: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    // 原因
    reason: '',
    reasonList: getDicList(DIC_KEY.obs_termination_reason),
    // 说明
    remark: '',
    rules: {
      reason: {
        required: true
      },
    },
    message: {
      reason: {
        required: '请选择终止原因'
      },

    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    changeRadioGroup(e) {
      this.setData({
        reason: e.detail.value
      })
    },
    submit(callback) {
      const {
        reason,
        remark,
        orderNum
      } = this.data
      const params = {
        reason,
        remark,
        orderNum
      }
      const validate = new WxValidate(this.data.rules, this.data.message)

      if (!validate.checkForm(params)) {
        const error = validate.errorList[0]
        wx.showToast({
          title: error.msg,
          icon: 'error'
        })
        return false
      }
      WorkOrderMyIndexApi.termination(params, ({
        code,
        msg
      }) => {
        if (code === 200) {
          wx.showToast({
            title: '提交成功',
            icon: "success"
          })
          wx.$_.delay(callback, 1000)

        } else {
          wx.showToast({
            title: msg,
            icon: 'error'
          })
        }
      })
    },
  }
})